package com.loozb.thread4.queue;


import java.text.MessageFormat;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 消费者
 * @author 龙召碧
 * @create 2018-02-06 9:55
 **/
public class Consumer implements Runnable{
    private BlockingQueue<PCData> queue;
    private static final int SLEEPTIME = 1000;

    public Consumer(BlockingQueue<PCData> queue) {
        this.queue = queue;
    }
    @Override
    public void run() {
        Random r = new Random();
        System.out.println("start producer id ="+Thread.currentThread().getId());
        try {
            while(true) {
                //获取队列里的数据
                PCData data = queue.take();
                if(null != data) {
                    int re = data.getData() * data.getData();//计算平方
                    System.out.println(MessageFormat.format("{0}*{1}={2}", data.getData(), data.getData(), re));
                    Thread.sleep(r.nextInt(SLEEPTIME));
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt();
        }
    }

}
